﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcAtletIS.Models;

namespace MvcAtletIS.Controllers
{
    public class SignupsController : Controller
    {
        private atletisEntities db = new atletisEntities();
        
        // GET: /Signups/
        public ActionResult Index(string status = "")
        {
            DateTime today = DateTime.Now;
            var competitions = db.Competitions.Where(c => c.Deadline >= today & c.External == false & c.Public == true).ToList();
            var disciplineList = db.DisciplineList.Where(dl => dl.CompetitionId == 0).OrderBy(dl => dl.ExternalId).ToList();
            var countries = db.Countries.OrderBy(c => c.Name).ToList();

            ViewBag.CompetitionId = new SelectList(competitions, "ID", "Name");
            ViewBag.DisciplineListId = new SelectList(disciplineList, "ID", "Name");
            ViewBag.CountryId = new SelectList(countries, "ID", "Name", "SVK");
            ViewBag.Status = status;
            
            return View();
        }

        [HttpPost]
        public ActionResult Index(Performances model, int CompetitionId = 0, int DisciplineListId = 0)
        {
            model.CompetitorId = 0;
            model.StartNumber = 0;
            model.Lane = 0;
            model.Code = 0;
            model.SB = 0;   // SB sa nacitavaju len pre registrovanych pretekarov
            model.Result = 0;
            model.AdvResult = 0;
            model.SecondaryRank = 0;
            model.Reaction = string.Empty;
            model.SMS = string.Empty;
            model.Points = 0;
            model.Active = true;
            model.Sync = true;
            model.Rank = 0;
            
            if (ModelState.IsValid)
            {
                db.Performances.AddObject(model);
                db.SaveChanges();

                model = new Performances();

                return RedirectToAction("Index", new { status = "Vaše prihlásenie bolo úspešné!" });
            }

            var competitions = db.Competitions.ToList();
            var disciplineList = db.DisciplineList.Where(dl => dl.CompetitionId == 0).ToList();
            var countries = db.Countries.OrderBy(c => c.Name).ToList();

            ViewBag.CompetitionId = new SelectList(competitions, "ID", "Name", CompetitionId);
            ViewBag.DisciplineListId = new SelectList(disciplineList, "ID", "Name", DisciplineListId);
            ViewBag.CountryId = new SelectList(countries, "ID", "Name");

            return View(model);
        }

        // GET: /Signups/List
        public ViewResult List(int CompetitionId = 0, int DisciplineListId = 0)
        {
            DateTime today = DateTime.Now;
            var competitions = db.Competitions.Where(c => c.Deadline >= today.Date & c.External == false).ToList();
            var disciplineList = db.DisciplineList.Where(dl => dl.CompetitionId == CompetitionId & dl.ParentId == 0).ToList();

            ViewBag.CompetitionId = new SelectList(competitions, "ID", "Name", CompetitionId);
            ViewBag.DisciplineListId = new SelectList(disciplineList, "ID", "Name", DisciplineListId);

            var model = db.Performances.Include("Countries").Where(p => p.DisciplineListId == DisciplineListId).OrderBy(p => p.Club).OrderBy(p => p.Surname).ToList();

            return View(model);
        }

        #region CascadeDropDown
        
        private IList<DisciplineList> GetDisciplineList(int id)
        {
            return db.DisciplineList.Where(dl => dl.CompetitionId == id & dl.ParentId == 0).OrderBy(dl => dl.ExternalId).ToList();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadDisciplinesForCompetition(string id)
        {
            var disciplineList = this.GetDisciplineList(Convert.ToInt32(id));

            var disciplineData = disciplineList.Select(m => new SelectListItem()
            {
                Text = m.Name,
                Value = m.ID.ToString(),

            });

            return Json(disciplineData, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
